// Package stock
// Author liuzhiyong
// Date 2023/11/17
// Description: 业务层代码
package stock

import (
	"context"
	"gitee.com/liu_zy_developer/go_stock_service/dao/mysql"
	"gitee.com/liu_zy_developer/go_stock_service/proto"
)

// GetStockByGoodsId 查询商品库存
func GetStockByGoodsId(ctx context.Context, goodsId int64) (*proto.GoodsStockInfo, error) {
	// 查mysql,根据goodsId 查询库存
	data, err := mysql.GetStockByGoodsId(ctx, goodsId)
	if err != nil {
		return nil, err
	}
	// 处理数据
	resp := &proto.GoodsStockInfo{
		GoodsId: data.GoodsId,
		Num:     data.Num,
	}
	return resp, nil
}

// ReduceStock 扣减库存
func ReduceStock(ctx context.Context, goodsId int64, num int64) (*proto.GoodsStockInfo, error) {
	// 扣减库存
	data, err := mysql.ReduceStock(ctx, goodsId, num)
	if err != nil {
		return nil, err
	}
	// 处理数据
	resp := &proto.GoodsStockInfo{
		GoodsId: data.GoodsId,
		Num:     data.Num,
	}
	return resp, nil
}
